package com.mall.shopping.services;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: mjm
 * @Date: 2021/12/13
 * @Time: 10:11
 * @Description:
 */
@Service
public class ProductServiceImpl implements IProductService {


    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ProductCateConverter productCateConverter;
    @Autowired
    ItemDescMapper itemDescMapper;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        ProductDetailDto productDetailDto = new ProductDetailDto();
        //参数校验
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = null;
            if (item.getId() != null) {
                itemDesc = itemDescMapper.selectByPrimaryKey(item.getId());
            }
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailDto.setProductId(item.getId());
            productDetailDto.setLimitNum(item.getLimitNum());
            productDetailDto.setSalePrice(item.getPrice());
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setProductImageSmall(Arrays.asList(item.getImage()));
            productDetailDto.setProductName(item.getTitle());
            productDetailDto.setSubTitle(item.getSellPoint());
            productDetailResponse.setProductDetailDto(productDetailDto);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (ValidateException e) {
            productDetailResponse.setCode(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getMessage());
            return productDetailResponse;
        } catch (Exception e) {
            productDetailResponse.setCode(ShoppingRetCode.SYSTEM_TIMEOUT.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SYSTEM_TIMEOUT.getMessage());
            return productDetailResponse;
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            //参数校验
            request.requestCheck();

            Long cid = request.getCid();
            Integer page = request.getPage();
            Integer priceGt = request.getPriceGt();
            Integer priceLte = request.getPriceLte();
            Integer size = request.getSize();
            String sort = request.getSort();
            //在数据库查询前生效
            PageHelper.startPage(page, size);

            //根据商品id排序
            List<Item> itemList = null;
            if (priceGt == null && priceLte != null) {
                priceGt = 1;
            } else if (priceGt != null && priceLte == null) {
                priceLte = 999999999;
            }
            if ("-1".equals(sort)) {
                sort = "desc";
                itemList = itemMapper.selectItemFront(cid, "price", sort, priceGt, priceLte);
            } else {
                sort = "asc";
                itemList = itemMapper.selectItemFront(cid, "price", sort, priceGt, priceLte);
            }
            PageInfo<Item> info = new PageInfo<>(itemList);
            List<ProductDto> productDtoList = productConverter.items2Dto(itemList);
            allProductResponse.setProductDtoList(productDtoList);
            allProductResponse.setTotal(info.getTotal());

            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            allProductResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return allProductResponse;
    }


    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        Set<PanelDto> panelDtoSet = new HashSet<>();

        try {
            //查询tb-panel表热门推荐商品
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("name", "热门推荐");
            List<Panel> panelList = panelMapper.selectByExample(example);
            //只能查出一条记录
            Panel panel = panelList.get(0);
            PanelDto panelDto = contentConverter.panel2Dto(panel);


            //根据tb_panel表中的id查询tb_panel_content中的商品
            //根据返回的productid查询具体商品信息
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelDto.getId());
            //PanelContentItem和PanelContentItemDto字段一模一样。。。。。
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);

            panelDtoSet.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtoSet);

            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return recommendResponse;
    }
}
